﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.Admin.ServiceReference1;
namespace CMP.ERP.Admin.Controllers
{
    public class OrderController : Controller
    {
        //
        // GET: /Order/
        ServiceReference1.Service1Client client = new Service1Client();
        public ActionResult Order_Show()
        {

            ViewBag.DHLUPSFEDEXEMSID = new SelectList(client.GetDHLUPSFEDEXEMSInfo(), "DHLUPSFEDEXEMSID", "DHLUPSFEDEXEMSName");
            ViewBag.StorehouseID = new SelectList(client.Storehouseshow(), "StorehouseID", "StorehouseName");
            ViewBag.StoreID = new SelectList(client.ShowStoreInfo(), "StoreID", "StoreName");
            return View();
        }
        /// <summary>
        /// 根据商品编码获取商品信息
        /// </summary>
        /// <param name="GoodsCoding"></param>
        /// <returns></returns>
        public ActionResult GetGoodsByGoodsCoding(string GoodsCoding)
        {

            List<GoodsInfo> list = client.GetGoodsByGoodsCoding(GoodsCoding).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加订单信息
        /// </summary>
        /// <param name="GoodsCoding"></param>
        /// <returns></returns>
        public ActionResult AddOrder(OrderInfo oo, FormCollection ff)
        {

            oo.InOrderTime = DateTime.Now;
            oo.UserID = Convert.ToInt32(Session["UserID"]);
            oo.OrderCoding = System.Guid.NewGuid().ToString();
            oo.OrderState = 0;
            if (ff["GoodsSum"] == null || ff["Price"] == null || ff["GoodsID"] == null)
            {
                return Content("<script>alert('数据不完整！');location.href='/Order/Order_Show'</script>");
            }
            else
            {
                string[] GoodsSum = ff["GoodsSum"].ToString().Split(',');
                string[] Price = ff["Price"].ToString().Split(',');
                string[] GoodsID = ff["GoodsID"].ToString().Split(',');
                string xml = "<ss>";
                for (int i = 0; i < GoodsSum.Length; i++)
                {
                    xml += "<cc  GoodsID='" + GoodsID[i] + "' GoodsSum='" + GoodsSum[i] + "' Price='" + Price[i] + "'/>";
                }
                xml += "</ss>";

                if (client.AddOrderInfo(oo, xml) > 0)
                {
                    return Content("<script>alert('下单成功！'); location.href='/Order/Order_details'</script>");
                }
                else
                {

                    return Content("<script>alert('服务器忙！');</script>");
                }
            }
        }
        public ActionResult Order_details()
        {
            List<OrderInfo> list = client.ShowOrderInfo().ToList();
            return View(list);
        }
        public ActionResult Order_cc(int id)
        {
            ViewBag.OrderSonInFo = client.GetOrderInfoByid(id);
            return View(client.oo(id).ToList());
        }
        /// <summary>
        /// 确认审核（增加库存量）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Add_ruku(int id)
        {
            if (client.GetByOrder(id) == 0)
            {
                if (client.Add_ruku(id) > 0)
                {
                    return Content("<script>alert('审核成功！'); location.href='/Order/Order_Show'</script>");
                }
                else
                {
                    return Content("<script>alert('服务器忙！'); location.href='/Order/Order_cc/id'</script>");
                }
            }
            else
            {
                return Content("<script>alert('服务器忙！'); location.href='/Order/Order_cc/id'</script>");
            }
            return Content("<script>alert('不能在审核了！');location.href='/Order/Order_details'</script>");
        }
        public ActionResult SelShow()
        {
            return View();
        }
        /// <summary>
        /// 年，月，日，报表
        /// </summary>
        /// <param name="StartTime"></param>
        /// <returns></returns>
        public ActionResult SelYear(string StartTime, string EndTime)
        {
            List<OrderInfo> o = client.SelYear(StartTime, EndTime).ToList();
            return Json(o, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SelMonth(string StartTime, string EndTime, string YearTime)
        {
            List<OrderInfo> o = client.SelMonth(StartTime,EndTime,YearTime).ToList();
            return Json(o, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SelDay(string StartTime, string EndTime, string YearTime, string MonthTime)
        {
            List<OrderInfo> o = client.SelDay(StartTime, EndTime, YearTime,MonthTime).ToList();
            return Json(o, JsonRequestBehavior.AllowGet);
        }
    }
}
